/***************************************************************************
 创建者: hualei
 开始时间: 2018.9.10
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "batterymonitor.h"
#include <qdebug.h>
#include <qdom.h>
#include "domparser.h"
#include "domparser_global.h"
#include <QObject>
#include <QTime>
#include <QCoreApplication>
#include <QSerialPortInfo>
#include "serialstructdefine.h"
#include "robotcontrol.h"
#include "modbus.h"

#define IF_DEBUG (1)

extern RobotControl* robCtl;

BatteryMonitor::BatteryMonitor(QString configFileName)
{
    configFilePath=configFileName;

    isInitialOkStatus = false;
    batteryStatus = E_BATTERY_STATUS_NORMAL;
    requestCount=0;
    debugFlag=0;

    modbusMaster=NULL;

    batteryInformation_modbus.errorStatus=0;
    batteryInformation_modbus.batteryCurrentCurrency=0;
    batteryInformation_modbus.batteryLeftLightmodRatio=1.0;

    batteryInformation_can.errorStatus=0;
    batteryInformation_can.batteryCurrentCurrency=0;
    batteryInformation_can.batteryLeftLightmodRatio=1.0;


    serialPortConfig = new SerialPortConfig;
    batteryPeak = new BatteryPeakConfig;
    serialPort_device = new QSerialPort;
    // 初始化配置文件
    int tmpKey=initialBatteryByConfigFile(configFileName);
    if(1==tmpKey && 1==isUseBattery && 0==batteryBusType)//串口接口电池
    {
        // 初始化串口
        tmpKey=initalSerialPort();
        if(1==tmpKey)
        {
            isInitialOkStatus = true;
        }
        // 电池组通讯命令
        BatteryInformation batteryInformation;
        batteryInformation.errorStatus=0;
        batteryInformation.batteryLeftLightmodRatio=1.0;
        batteryCommandVec << 0x08 << 0x09 << 0x0a << 0x0d << 0x0f << 0x17 << 0x18 << 0x24;

        for( itBatteryDeviceMap = batteryDeviceMap.begin();\
             itBatteryDeviceMap != batteryDeviceMap.end();\
             itBatteryDeviceMap++)
        {
            mutex.lock();
            batteryInformationMap.insert(itBatteryDeviceMap.key(), batteryInformation);
            mutex.unlock();
        }

        itCommandVec = batteryCommandVec.begin();
        itBatteryDeviceMap = batteryDeviceMap.begin();

        if(serialPort_device->isOpen())
        {
//            timer.start( serialPortConfig->updatePeriod );
            timer.start( 1600);
        }
        QObject::connect(&timer, SIGNAL( timeout() ), this, SLOT(timeOut_slot()));
        QObject::connect(serialPort_device, SIGNAL(readyRead() ), this, SLOT(receiveData()));


    }
    else if(1==tmpKey && 1==isUseBattery && (1==batteryBusType||3==batteryBusType))//can接口电池
    {
        isInitialOkStatus = true;
        timer.start( 500);
        QObject::connect(&timer, SIGNAL( timeout() ), this, SLOT(timeOut_slot()));

    }
    else if(1==tmpKey && 1==isUseBattery && 2==batteryBusType)//modbus接口电池
    {
        if(1==initialModbusMaster(serialPortConfig,batteryBusAlias))
        {

            itBatteryDeviceMap = batteryDeviceMap.begin();


            isInitialOkStatus = true;
            timer.start( 500);
            QObject::connect(&timer, SIGNAL( timeout() ), this, SLOT(timeOut_slot()));
        }


    }




}

int BatteryMonitor::getBatteryPeakConfig(BatteryPeakConfig &configOut)
{
    configOut=*batteryPeak;
    return 1;
}

int BatteryMonitor::setBatteryPeakConfig(BatteryPeakConfig configIn)
{
    *batteryPeak=configIn;
    return writeConfigFile(configFilePath,configIn);

}

int BatteryMonitor::setDebugFlag(int debugFlagIn)
{
    debugFlag=debugFlagIn;
    return 1;
}

int BatteryMonitor::getBatteryInfo(BatteryInformation &batteryInfoOut)
{
    if(0==isUseBattery)
    {
        batteryInfoOut.batteryLeftLightmodRatio=-100;
        batteryInfoOut.errorStatus=0;
        return 0;
    }

    if(0==batteryBusType)
    {
        if(false==isInitialOkStatus || requestCount>100)
        {
            batteryInfoOut.batteryLeftLightmodRatio=-100;
            return -requestCount;
        }
        mutex.lock();
        batteryInfoOut=batteryInformationMap[itBatteryDeviceMap.key()];
        mutex.unlock();
    }
    else if(1==batteryBusType || 3==batteryBusType)
    {
        mutex.lock();
        batteryInfoOut=batteryInformation_can;
        mutex.unlock();
    }
    else if(2==batteryBusType)
    {
        if(false==isInitialOkStatus || requestCount>100)
        {
            batteryInfoOut.batteryLeftLightmodRatio=-100;
            return -requestCount;
        }
        mutex.lock();
        batteryInfoOut=batteryInformation_modbus;
        mutex.unlock();
    }

    return 1;
}

// 配置文件初始化状态
bool BatteryMonitor::isInitalOk()
{
    if(0==isUseBattery)
    {
        return true;
    }
    return isInitialOkStatus;
}

// 初始化电池配置文件
int BatteryMonitor::initialBatteryByConfigFile(QString configFileName)
{
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 初始化配置文件：" << configFileName;

    DomParser domparser;
    bool ok = domparser.openXml(configFileName, QIODevice::ReadOnly);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 配置文件打开失败：" << configFileName;
        }
        return -1;
    }

    QDomNode domNode = domparser.findSubNode(domparser.getRootDomElement(), "isUseBattery", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取结点失败：" << configFileName;
        }
            return -1;
    }
    domparser.readXml(domNode, isUseBattery);
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== isUseBattery" << isUseBattery;

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "shutdownDelaySeconds", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取结点失败：shutdownDelaySeconds" << configFileName;
        }
            return -101;
    }
    domparser.readXml(domNode, shutdownDelaySeconds);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "minBatteryVoltage", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取结点失败：minBatteryVoltage" << configFileName;
        }
            return -101;
    }
    domparser.readXml(domNode, minBatteryVoltage);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "maxBatteryVoltage", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ====读取结点失败： maxBatteryVoltage" << configFileName;
        }
            return -101;
    }
    domparser.readXml(domNode, maxBatteryVoltage);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "batteryBusType", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取batteryBusType结点失败：" << configFileName;
        }
            return -1;
    }
    domparser.readXml(domNode, batteryBusType);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "batteryBusAlias", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取结点失败：" << configFileName;
        }
            return -1;
    }
    domparser.readXml(domNode, batteryBusAlias);

    domNode = domparser.findSubNode(domparser.getRootDomElement(), "BatteryPeak", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取BatteryPeak结点失败：" << configFileName;
        }
            return -1;
    }

    // 读取充电时间（单位：min）
    domparser.readXml(domparser.findSubNode(domNode, "chargeTime", ok ), batteryPeak->chargeTime);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取chargeTime失败：" << configFileName;
        }
            return -1;
    }
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取chargeTime：" << batteryPeak->chargeTime;

    // 读取充电标准的电池组相对容量比(单位：％)
    domparser.readXml(domparser.findSubNode(domNode, "chargeVolume", ok ), batteryPeak->chargeVolume);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取chargeVolume失败：" \
                     << configFileName;
        }
            return -1;
    }
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取chargeVolume：" << batteryPeak->chargeVolume;

    // 读取警报时的电池组相对容量比（单位：％）
    domparser.readXml(domparser.findSubNode(domNode, "warnningBatteryLeftLightmode", ok ), \
                      batteryPeak->warnningBatteryLeftLightmode);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取warnningBatteryLeftLightmode失败："\
                     << configFileName;
        }
            return -1;
    }
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取warnningBatteryLeftLightmode:" \
             << batteryPeak->warnningBatteryLeftLightmode;

    // 读取需要充电的电池组相对容量比(单位：％)
    domparser.readXml(domparser.findSubNode(domNode, "goChargeBatteryLeftLightmode", ok ), \
                      batteryPeak->goChargeBatteryLeftLightmode);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取goChargeBatteryLeftLightmode失败："\
                     << configFileName;
        }
            return -1;
    }
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取goChargeBatteryLeftLightmode:" \
             << batteryPeak->goChargeBatteryLeftLightmode;

    // 读取需要关机时的电池组相对容量比(单位：％)
    domparser.readXml(domparser.findSubNode(domNode, "shutDownBatteryLeftLightmode", ok ), \
                      batteryPeak->shutDownBatteryLeftLightmode);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取shutDownBatteryLeftLightmode失败："\
                     << configFileName;
        }
            return -1;
    }
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取shutDownBatteryLeftLightmode:"\
             << batteryPeak->shutDownBatteryLeftLightmode;

    //　读取串口配置参数结点
    domNode = domparser.findSubNode(domparser.getRootDomElement(), "SerialSetting", ok );
    if(!ok)
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取SerialSetting失败："\
                     << configFileName;
        }
        return -100;
    }

    domparser.readXml( domparser.findSubNode(domNode, "busType", ok), \
                       serialPortConfig->busType );
    if( !ok  )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口busType失败："\
                     << configFileName;
        }
        return -2;
    }
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口busType:" \
             << serialPortConfig->busType;

    // 读取串口设备文件
    domparser.readXml(domparser.findSubNode(domNode, "serialDeviceName", ok),\
                      serialPortConfig->serialDeviceName );
    if( !ok  )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口serialDeviceName失败:" \
                     << configFileName;
        }
        return -3;
    }
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取serialDeviceName:" \
             << serialPortConfig->serialDeviceName;

    // 读取isUseUsb
    domparser.readXml(domparser.findSubNode(domNode, "isUseUsbDeviceSerialCode", ok),\
                      serialPortConfig->isUseUsbDeviceSerialCode );
    if( !ok  )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口isUseUsbDeviceSerialCode失败:" \
                     << configFileName;
        }
        return -3;
    }
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取isUseUsbDeviceSerialCode:" \
             << serialPortConfig->isUseUsbDeviceSerialCode;

    // 读取配置文件波特率
    int baud;
    domparser.readXml(domparser.findSubNode(domNode, "baud" ,ok), baud);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口baud失败:"\
                     << configFileName;

        }
        return -4;
    }
    serialPortConfig->baudRate = (QSerialPort::BaudRate)baud;
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口baud:" \
             << serialPortConfig->baudRate;

    // 读取配置文件校验位
    QString parity;
    int parityNumber;
    domparser.readXml(domparser.findSubNode(domNode, "parity", ok), parity);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口parity失败:"\
                     << configFileName;
        }
        return -5;
    }

    if(parity == "NONE")
    {
        parityNumber = 0;
    }
    else if (parity == "EVEN")
    {
        parityNumber = 2;
    }
    else if (parity == "ODD")
    {
        parityNumber= 3;
    }
    else
    {
        qDebug()  << "BatteryMonitor::initialBatteryByConfigFile ==== 校验位设置错误:" \
                  << configFileName;
        return -6;
    }
    serialPortConfig->parity = (QSerialPort::Parity)parityNumber;
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口parity:"\
             << serialPortConfig->parity;

    //　读取配置文件数据位
    int dataBits;
    domparser.readXml(domparser.findSubNode(domNode, "data_bit", ok), dataBits);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口data_bit失败:"\
                     << configFileName;
        }
        return -7;
    }
    serialPortConfig->dataBits = (QSerialPort::DataBits)dataBits;
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口data_bit:"\
             << serialPortConfig->dataBits;

    // 读取配置文件停止位
    int stopBits;
    domparser.readXml(domparser.findSubNode(domNode, "stop_bit", ok), stopBits );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口stop_bit失败:" \
                     << configFileName;
        }
        return -8;
    }
    serialPortConfig->stopBits = (QSerialPort::StopBits)stopBits;
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口stop_bit:" \
             << serialPortConfig->stopBits;

    domparser.readXml(domparser.findSubNode(domNode, "updatePeriod", ok), serialPortConfig->updatePeriod);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口updatePeriod失败:"\
                     << configFileName;
        }
        return -9;
    }
    qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取串口updatePeriod:" \
             << serialPortConfig->updatePeriod;

    //　读取设备节点
    QDomNode deviceNode = domparser.findSubNode(domparser.getRootDomElement(), "NodeInfo", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug()<<"BatteryMonitor::initialBatteryByConfigFile ==== 读NodeInfo失败: "\
                   << configFileName;
        }
        return -10;
    }
    BatteryDevice batteryDevice;
    QDomNodeList domNodeList = deviceNode.childNodes();
    for(int i = 0; i < domNodeList.count(); i++)
    {
        domparser.readXml(domparser.findSubNode(domNodeList.at(i),"batteryVendorId",ok), \
                          batteryDevice.batteryVendorId);
        if( !ok )
        {
            if(IF_DEBUG)
            {
                qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取batteryVendorId失败："\
                         << configFileName;
            }
            return -1;
        }
        qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取batteryVendorId:"\
                 << batteryDevice.batteryVendorId;



        domparser.readXml(domparser.findSubNode(domNodeList.at(i),"batteryDeviceId",ok), \
                          batteryDevice.batteryDeviceId);
        if( !ok )
        {
            if(IF_DEBUG)
            {
                qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取batteryDeviceId失败："\
                         << configFileName;
            }
            return -1;
        }
        qDebug() << "BatteryMonitor::initialBatteryByConfigFile ==== 读取batteryDeviceId:" \
                 << batteryDevice.batteryDeviceId;


        mutex.lock();
        batteryDeviceMap.insert(batteryDevice.batteryDeviceId,batteryDevice);
        mutex.unlock();

        int batteryKey = batteryDevice.batteryDeviceId;
        qDebug() << "batteryDeviceMap::key:" << batteryKey << " === data:" \
                 << batteryDeviceMap.value(batteryKey).batteryVendorId;
    }
    batteryConfigFilePath = configFileName;

    domparser.closeXml();
    return 1;
}

/**
 * @brief BatteryMonitor::timeOut_slot
 */
void BatteryMonitor::timeOut_slot()
{
    if(1==debugFlag)
    {
        qDebug()<<"BatteryMonitor::timeOut_slot() In";
    }
    batteryMonitorLoop();
    if(1==debugFlag)
    {
        qDebug()<<"BatteryMonitor::timeOut_slot() Out";
    }
}

/**
 * @brief BatteryMonitor::batteryMonitorLoop
 * @return
 */
int BatteryMonitor::batteryMonitorLoop()
{
//    qDebug() << "BatteryMonitor::batteryMonitorLoop ==== 发送指令设备厂商ＩＤ:" \
//             << batteryDeviceMap.value( itBatteryDeviceMap.key()).batteryVendorId;
//    qDebug() << "BatteryMonitor::batteryMonitorLoop ==== 发送指令设备地址:" << itBatteryDeviceMap.key();
    //update status

    if(0==batteryBusType)
    {
        mutex.lock();
        double lightModeRatio=batteryInformationMap[itBatteryDeviceMap.key()].batteryLeftLightmodRatio ;
        updateBatteryStatus(lightModeRatio,batteryStatus);
        sendGetValueCommand( itBatteryDeviceMap.key() );
        mutex.unlock();
    }
    else if(1==batteryBusType)
    {
        mutex.lock();
        if(E_BATTERY_VENDOR_ID_SERVODRIVER_VOLTAGE==batteryDeviceMap.first().batteryVendorId)
        {
            robCtl->getBatteryInfo_fromMotor(batteryBusAlias,batteryInformation_can);
            //急停会断开驱动器供电
            if(batteryInformation_can.batterySumVoltage>10)
            {

                batteryInformation_can.batteryLeftLightmodRatio=
                        (batteryInformation_can.batterySumVoltage-minBatteryVoltage)/(maxBatteryVoltage-minBatteryVoltage);
            }

        }
        else if(E_BATTERY_VENDOR_ID_SERVODRIVER_VOLUME==batteryDeviceMap.first().batteryVendorId)
        {
            robCtl->getBatteryInfo_fromMotor(batteryBusAlias,batteryInformation_can);

        }
        else
        {
             robCtl->getBatteryInfo(batteryBusAlias,batteryInformation_can);
        }

        updateBatteryStatus(batteryInformation_can.batteryLeftLightmodRatio,batteryStatus);
        mutex.unlock();
    }
    else if(2==batteryBusType)
    {
        mutex.lock();
        if(1==modbusBatteryCommunicateLoop(batteryInformation_modbus))
        {
            requestCount=0;//复位计数器。
        }
        updateBatteryStatus(batteryInformation_modbus.batteryLeftLightmodRatio,batteryStatus);
        mutex.unlock();
    }
    else if(3==batteryBusType)
    {
        mutex.lock();
        if(E_BATTERY_VENDOR_ID_AI==batteryDeviceMap.first().batteryVendorId)
        {
            double tmpAi;
            robCtl->getRobotAi(1,batteryBusAlias,tmpAi);
            if(tmpAi>10)
            {

                batteryInformation_can.batteryLeftLightmodRatio=
                        (tmpAi-minBatteryVoltage)/(maxBatteryVoltage-minBatteryVoltage);
            }
//            qDebug()<<"tmpAi"<<tmpAi<<"batteryLeftLightmodRatio"<<batteryInformation_can.batteryLeftLightmodRatio;

        }


        updateBatteryStatus(batteryInformation_can.batteryLeftLightmodRatio,batteryStatus);
        mutex.unlock();
    }


    return 1;
}


/**
 * @brief BatteryMonitor::sendGetValueCommand   发送获取数据命令
 * @param batteryModuleId   　接收命令设备Id
 * @return
 */
int BatteryMonitor::sendGetValueCommand(int batteryModuleId)
{

//    qDebug() << "BatteryMonitor::sendGetValueCommand ==== 发送指令设备厂商Id:" \
//             << batteryDeviceMap.value( batteryModuleId).batteryVendorId;

    QByteArray sendCommand;
    switch ( batteryDeviceMap.value(batteryModuleId).batteryVendorId )
    {
    case E_BATTERY_VENDOR_ID_GP07SC_VER01:
        GP07SC_VER01_Command(sendCommand);
        break;
    case E_BATTERY_VENDOR_ID_shangHaiJinQu:
        shangHaiJinQu_Command(sendCommand);
        break;
    case E_BATTERY_VENDOR_ID_shenZhengAoYouNeng:
        shenZhengAoYouNeng_Command(sendCommand);
        break;
    case E_BATTERY_VENDOR_ID_PeiChengChuNeng:
        sendRequestCommand_PeiChengChuNeng(sendCommand);
        break;
    case E_BATTERY_VENDOR_ID_ShangDongJongGong:
        sendRequestCommand_ShangDongJingGong(sendCommand);
        break;

    default:
        break;
    }

    serialPort_device->write(sendCommand);

    requestCount++;

    return 1;
}

/**
 * @brief BatteryMonitor::GP07SC_VER01_Command
 * @param sendCommand
 */
void BatteryMonitor::GP07SC_VER01_Command(QByteArray &sendCommand)
{
    sendCommand.resize(9);
    sendCommand[0] = 0x3a;                      //起始标识
    sendCommand[1] = itBatteryDeviceMap.key();  //设备地址
    sendCommand[2] = *itCommandVec;             //通讯命令
    sendCommand[3] = 0x01;                      //数据长度
    sendCommand[4] = 0x0b;                      //发送指令数据内容
    sendCommand[5] = (sendCommand[1] + sendCommand[2]) + (sendCommand[3] + sendCommand[4]);  //数据校验和，低字节
    sendCommand[6] = 0x00;                      //数据校验和，高地址
    sendCommand[7] = 0x0d;                      //结束标识１
    sendCommand[8] = 0x0a;                      //结束标识２
}

void BatteryMonitor::shangHaiJinQu_Command(QByteArray &sendCommand)
{
    sendCommand.resize(7);
    sendCommand[0] = 0xdd;                      //起始标识
    sendCommand[1] = 0xa5;                      //读
    sendCommand[2] = 0x03;                      //功能码
    sendCommand[3] = 0x00;                      //数据长度
    sendCommand[4] = 0xff;                      //数据校验 高位
    sendCommand[5] = 0xfd;                      //数据校验　低位
    sendCommand[6] = 0x77;                      //结束标识

}

void BatteryMonitor::shenZhengAoYouNeng_Command(QByteArray &sendCommand)
{
    sendCommand.resize(12);
    sendCommand[0] = 0x7e;                      //起始标识
    sendCommand[1] = valueToAscii(itBatteryDeviceMap.key()>>4);  //设备地址
    sendCommand[2] = valueToAscii(itBatteryDeviceMap.key()&0x0f);  //设备地址
    sendCommand[3] = valueToAscii(0x04);             //通讯命令 46h
    sendCommand[4] = valueToAscii(0x06);             //通讯命令 46h
    sendCommand[5] = valueToAscii(0x04);             //通讯命令 41h
    sendCommand[6] = valueToAscii(0x01);             //通讯命令 41h
    sendCommand[7] = valueToAscii(0x00);                      //发送指令长度　０
    sendCommand[8] = valueToAscii(0x00);                      //发送指令长度　０
//    //求和，模２５６的余数　高字节，低字节。
//    unsigned int tmpSum=0;
//    for(int i=0;i<6;i++)
//    {
//        tmpSum+=sendCommand[1+i];
//    }
//    tmpSum=tmpSum % 256;
//    sendCommand[9] = charToHex(tmpSum>>4);
//    sendCommand[10] = charToHex(tmpSum&0x0f);
    //校验不对
    sendCommand[9] = 0x38;
    sendCommand[10] = 0x46;
    sendCommand[11] = 0x0d;                      //结束标识１

}

void BatteryMonitor::sendRequestCommand_PeiChengChuNeng(QByteArray &sendCommand)
{
    //7E 32 35 30 30 34 36 34 32 45 30 30 32 30 31 46 46 43 46 0D
//    unsigned char ver,adr,cid1,cid2,length,info,checksum,eoi;
//    ver=0;//?
//    length=3;//?
//    info=0xff;
//    adr=itBatteryDeviceMap.key();
//    cid1=0x46;
//    cid2=0x42;//命令字,获取pack模拟量
//    eoi=0x0d;

//    sendCommand.resize(12);
//    sendCommand[0] = 0x7e;                      //起始标识
//    sendCommand[1] = ver;                      //Ver
//    sendCommand[2] = adr;
//    sendCommand[3] = cid1;
//    sendCommand[4] = cid2;
//    sendCommand[5] = valueToAscii(length>>4);  //设备地址
//    sendCommand[6] = valueToAscii(length&0x0f);  //设备地址
//    sendCommand[7] = info;
//    sendCommand[8] = valueToAscii(checksum>>4);
//    sendCommand[9] = valueToAscii(checksum&0x0f);
//    sendCommand[10] = eoi;

    sendCommand.resize(20);
    unsigned char tmpCommand[20]={0x7E,0x32 ,0x35 ,0x30 ,0x30 ,0x34 ,0x36 ,0x34 ,0x32 ,0x45 ,0x30 ,0x30 ,0x32 ,0x30 ,0x31 ,0x46 ,0x44 ,0x33 ,0x31 ,0x0D};

    for(int i=0;i<20;i++)
    {
        sendCommand[i]=tmpCommand[i];
    }


//    //数据求和（不要首尾和ｃｈｅｃｋsum)，模65536的余数+1,转换为４个ascia码　高字节，低字节。
//    unsigned int tmpSum=0;
//    for(int i=0;i<6;i++)
//    {
//        tmpSum+=sendCommand[1+i];
//    }
//    tmpSum=tmpSum % 256;
//    sendCommand[9] = charToHex(tmpSum>>4);
//    sendCommand[10] = charToHex(tmpSum&0x0f);

}

void BatteryMonitor::sendRequestCommand_ShangDongJingGong(QByteArray &sendCommand)
{
    static int tmpCount=0;

    if(0==tmpCount)
    {
        sendCommand.resize(8);
        //电压
        unsigned char tmpCommand[8]={0xEA,0xD1 ,0x01 ,0x04 ,0xFF ,0x02 ,0xF9 ,0xF5 };

        for(int i=0;i<8;i++)
        {
            sendCommand[i]=tmpCommand[i];
        }
//        qDebug()<<"sendRequestCommand_ShangDongJingGong 电压";
    }
    if(1==tmpCount)
    {
        sendCommand.resize(8);
        //电流
        unsigned char tmpCommand[20]={0xEA,0xD1 ,0x01 ,0x04 ,0xFF ,0x03 ,0xF8 ,0xF5 };

        for(int i=0;i<20;i++)
        {
            sendCommand[i]=tmpCommand[i];
        }
//        qDebug()<<"sendRequestCommand_ShangDongJingGong 电流";
    }
    if(2==tmpCount)
    {
        sendCommand.resize(8);
        //基本状态
        unsigned char tmpCommand[20]={0xEA,0xD1 ,0x01 ,0x04 ,0xFF ,0x04 ,0xFF ,0xF5 };

        for(int i=0;i<20;i++)
        {
            sendCommand[i]=tmpCommand[i];
        }
//        qDebug()<<"sendRequestCommand_ShangDongJingGong 基本状态";
    }


    tmpCount++;
    if(tmpCount==3)
    {
        tmpCount=0;
    }
}

unsigned char BatteryMonitor::valueToAscii(unsigned char bHex)
{
    if((bHex>=0)&&(bHex<=9))
    {
        bHex += 0x30;
    }
    else if((bHex>=10)&&(bHex<=15))//Capital
    {
        bHex += 0x37;
    }
    else
    {
        bHex = 0xff;
    }
    return bHex;
}

unsigned char BatteryMonitor::asciiToValue(unsigned char bChar)
{
    if((bChar>=0x30)&&(bChar<=0x39))
    {
        bChar -= 0x30;
    }
    else if((bChar>=0x41)&&(bChar<=0x46)) // Capital
    {
        bChar -= 0x37;
    }
    else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase
    {
        bChar -= 0x57;
    }
    else
    {
        bChar = 0xff;
    }
    return bChar;
}

int BatteryMonitor::modbusBatteryCommunicateLoop(BatteryInformation &batteryInformationOut)
{
    requestCount++;
    switch (batteryDeviceMap.value(itBatteryDeviceMap.key()).batteryVendorId)
    {
    case E_BATTERY_VENDOR_ID_KAWKER:
        return modbusBatteryCommunicateLoop_kawker(batteryInformationOut);
        break;
    default:
        break;
    }
    return 0;
}

int BatteryMonitor::modbusBatteryCommunicateLoop_kawker(BatteryInformation &batteryInformationOut)
{

    uint16_t data16List[20];

    int resultKey=modbus_read_registers(modbusMaster,14,7,data16List);
    if(-1!=resultKey)
    {

    }
    else
    {
        qDebug()<<"error, BatteryMonitor::modbusBatteryCommunicateLoop_kawker failed!!!"<<resultKey;
        return -1;
    }
    batteryInformationOut.batterySumVoltage=data16List[0]/100.0;
    int16_t tmpCurrency=data16List[1];
    batteryInformationOut.batteryCurrentCurrency=-tmpCurrency/100.0;//放电是正吗？华友要求：充电为正，放电为负
    batteryInformationOut.batteryTempture=((int16_t)data16List[2])-40;
    batteryInformationOut.batteryLeftLightmodRatio=data16List[6]*0.4/100.0;

    resultKey=modbus_read_registers(modbusMaster,67,1,data16List);
    if(-1!=resultKey)
    {

    }
    else
    {
        qDebug()<<"error, BatteryMonitor::modbusBatteryCommunicateLoop_kawker failed!!!"<<resultKey;
        return -1;
    }
    batteryInformationOut.errorStatus=data16List[0];
    return 1;

}

#define D_STAND_RECEIVE_LENGTH_AOYOU 130
/**
 * @brief BatteryMonitor::receiveData  接收数据
 * @return  success:1   failed:-1
 */
int BatteryMonitor::receiveData()
{
//    QTime dieTime = QTime::currentTime().addMSecs(200);
//    while( QTime::currentTime() < dieTime )
//    {
//        QCoreApplication::processEvents(QEventLoop::AllEvents, 200);
//    }

    //数据预判断
    int bufferSize=serialPort_device->bytesAvailable();
    QByteArray receiveBatteryData;
    switch (batteryDeviceMap.value(itBatteryDeviceMap.key()).batteryVendorId)
    {
    case E_BATTERY_VENDOR_ID_GP07SC_VER01:
    {
    }
        break;
    case E_BATTERY_VENDOR_ID_shangHaiJinQu:
    {
        if(34>bufferSize)
        {
            return 1;
        }
        receiveBatteryData.fill(0);
        receiveBatteryData = serialPort_device->readAll();
        int tmpReturn;
        tmpReturn=shangHaiJinQu_bufferCheck(receiveBatteryData);
        if(1!=tmpReturn)
        {
            return -1;
        }
    }
        break;
    case E_BATTERY_VENDOR_ID_shenZhengAoYouNeng:
    {
        if(D_STAND_RECEIVE_LENGTH_AOYOU>bufferSize)
        {
            return 1;
        }
        receiveBatteryData.fill(0);
        receiveBatteryData = serialPort_device->readAll();
        int tmpReturn;
        tmpReturn=shenZhengAoYouNeng_bufferCheck(receiveBatteryData);
        if(1!=tmpReturn)
        {
            return -1;
        }
    }
        break;
    case E_BATTERY_VENDOR_ID_PeiChengChuNeng:
    {
        if(50>bufferSize)//bufferSize不同电池结构不一样
        {
            return 1;
        }
        receiveBatteryData.fill(0);
        receiveBatteryData = serialPort_device->readAll();
        int tmpReturn;
        tmpReturn=bufferCheck_PeiChengChuNeng(receiveBatteryData);
        if(1!=tmpReturn)
        {
            return -1;
        }
        break;
    }     
    case E_BATTERY_VENDOR_ID_ShangDongJongGong:
    {
        if(500>bufferSize)//bufferSize不同电池结构不一样
        {
            return 1;
        }
//        qDebug()<<"111battery bufferSize"<<bufferSize;
        receiveBatteryData.fill(0);
        receiveBatteryData = serialPort_device->readAll();
//        int tmpReturn;
//        tmpReturn=bufferCheck_ShangDongJingGong(receiveBatteryData);
//        if(1!=tmpReturn)
//        {
//            return -1;
//        }
        break;
    }

    default:
        break;
    }


//    qDebug() << "BatteryMonitor::receiveData ==== 接收数据：" << receiveBatteryData.toHex();
//    qDebug() << "BatteryMonitor::receiveData ==== 接收数据设备:" << itBatteryDeviceMap.key();

    switch (batteryDeviceMap.value(itBatteryDeviceMap.key()).batteryVendorId)
    {
    case E_BATTERY_VENDOR_ID_GP07SC_VER01:
        GP07SC_VER01_ReceiveData(receiveBatteryData);
        break;
    case E_BATTERY_VENDOR_ID_shangHaiJinQu:
        shangHaiJinQu_ReceiveData(receiveBatteryData);
        break;
    case E_BATTERY_VENDOR_ID_shenZhengAoYouNeng:
        shenZhengAoYouNeng_ReceiveData(receiveBatteryData);
        break;
    case E_BATTERY_VENDOR_ID_PeiChengChuNeng:
    {
        receiveData_PeiChengChuNeng(receiveBatteryData);
        break;
    }
    case E_BATTERY_VENDOR_ID_ShangDongJongGong:
    {
        receiveData_ShangDongJingGong(receiveBatteryData);
        break;
    }
    default:
        break;
    }


    //chang deviceId
    if( itCommandVec != batteryCommandVec.end())
    {
        itCommandVec++;
//        qDebug() << "The next Command:" << *itCommandVec;
    }
    else
    {
       itCommandVec = batteryCommandVec.begin();
       if( itBatteryDeviceMap.key() == batteryDeviceMap.lastKey() )
       {
           mutex.lock();
           itBatteryDeviceMap = batteryDeviceMap.begin();
           mutex.unlock();
       }
    }
    return 1;
}

/**
 * @brief BatteryMonitor::GP07SC_VER01_ReceiveData
 * @param dataIn
 * @return
 */
int BatteryMonitor::GP07SC_VER01_ReceiveData(QByteArray &dataIn)
{
    unsigned char highByte = 0x00;
    unsigned char lowByte = 0x00;
    unsigned short data = 0;

    if( dataIn.at(2) != *itCommandVec )
    {
        qDebug() << "BatteryMonitor::receiveData() ==== 发送指令：" << *itCommandVec;
        qDebug() << "BatteryMonitor::receiveData() ==== 发送指令与接收数据指令不符："<< dataIn.at(2);
        return -1;
    }
    else
    {
        // 电池组内部温度，数据两字节（单位：卡尔文）
        if(dataIn.at(2) == 0x08)
        {
            if( dataIn.at(3) != 0x02)
            {
                qDebug() << "BatteryMonitor::receiveData ==== 电池组内部温度数据长度错误:" << dataIn.at(3);
                return -1;
            }
            else
            {
                double tempTure;
                // 数据低字节
                lowByte = dataIn.at(4);
                // 数据低字节
                highByte = dataIn.at(5);
                data |= highByte;
                data = (data << 8) | lowByte;
                tempTure = ((double)data - 2731.0)/10.0;
                mutex.lock();
                BatteryInformation batteryInfoTempture;

                mutex.lock();
                batteryInfoTempture = batteryInformationMap.value(itBatteryDeviceMap.key());
                mutex.unlock();
                batteryInfoTempture.batteryTempture = tempTure;

                mutex.lock();
                batteryInformationMap[itBatteryDeviceMap.key()] = batteryInfoTempture;
                mutex.unlock();
                qDebug() << "BatteryMonitor::receiveData ==== 电池组温度数据receiveBatteryData[4]："\
                         << dataIn.at(4);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组温度数据receiveBatteryData[5]："\
                         << dataIn.at(5);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组内部温度:" << tempTure;
            }
        }
        // 电池组总电压，数据两字节（单位：mV）
        else if(dataIn.at(2) == 0x09)
        {
            if( dataIn.at(3) != 0x02)
            {
                qDebug() << "BatteryMonitor::receiveData ==== 电池组总电压数据长度错误:" <<dataIn.at(3);
                return -1;
            }
            else
            {
                // 数据低字节
                lowByte = dataIn.at(4);
                // 数据低字节
                highByte = dataIn.at(5);
                data |= highByte;
                data = (data << 8) | lowByte;
                BatteryInformation batteryInfoSumVoltage;

                mutex.lock();
                batteryInfoSumVoltage = batteryInformationMap.value(itBatteryDeviceMap.key());
                mutex.unlock();
                batteryInfoSumVoltage.batterySumVoltage = (double)data;

                mutex.lock();
                batteryInformationMap[itBatteryDeviceMap.key()] = batteryInfoSumVoltage;
                mutex.unlock();

                qDebug() << "BatteryMonitor::receiveData ==== 电池组总电压receiveBatteryData[4]："\
                         << dataIn.at(4);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组总电压receiveBatteryData[5]："\
                         << dataIn.at(5);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组总电压:" << data;
            }
        }
        // 电池组实时电流，数据两字节（单位：mA）
        else if(dataIn.at(2) == 0x0a)
        {
            if( dataIn.at(3) != 0x02)
            {
                qDebug() << "BatteryMonitor::receiveData ==== 电池组实时电流数据长度错误:" \
                         << dataIn.at(3);
                return -1;
            }
            else
            {
                // 数据低字节
                lowByte = dataIn.at(4);
                // 数据低字节
                highByte = dataIn.at(5);
                data |= highByte;
                data = (data << 8) | lowByte;
                BatteryInformation batteryInfoCurrentCurrency;

                mutex.lock();
                batteryInfoCurrentCurrency = batteryInformationMap.value(itBatteryDeviceMap.key());
                mutex.unlock();
                batteryInfoCurrentCurrency.batteryCurrentCurrency = (double)data;

                mutex.lock();
                batteryInformationMap[itBatteryDeviceMap.key()] = batteryInfoCurrentCurrency;
                mutex.unlock();

                qDebug() << "BatteryMonitor::receiveData ==== 电池组实时电流receiveBatteryData[4]："\
                         << dataIn.at(4);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组实时电流receiveBatteryData[5]："\
                         << dataIn.at(5);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组实时电流:" << data;
            }
        }
        // 电池组相对容量百分比，数据一字节（单位：%）
        else if( dataIn.at(2) == 0x0d)
        {
            if( dataIn.at(3) != 0x01)
            {
                qDebug() << "BatteryMonitor::receiveData ==== 电池组相对容量百分比数据长度错误:"\
                         << dataIn.at(3);
                return -1;
            }
            else
            {
                lowByte = dataIn.at(4);
                data |= lowByte;

                BatteryInformation batteryInfoLeftLightmodRatio;

                mutex.lock();
                batteryInfoLeftLightmodRatio = batteryInformationMap.value(itBatteryDeviceMap.key());
                mutex.unlock();
                batteryInfoLeftLightmodRatio.batteryLeftLightmodRatio = (double)data;

                mutex.lock();
                batteryInformationMap[itBatteryDeviceMap.key()] = batteryInfoLeftLightmodRatio;
                mutex.unlock();

            }
        }
        // 电池组剩余容量，数据两字节（单位：mAh）
        else if(dataIn.at(2) == 0x0f)
        {
            if( dataIn.at(3) != 0x02 )
            {
                qDebug() << "BatteryMonitor::receiveData ==== 电池组剩余容量数据长度错误:"\
                         << dataIn.at(3);
                return -1;
            }
            else
            {
                // 数据低字节
                lowByte = dataIn.at(4);
                // 数据低字节
                highByte = dataIn.at(5);
                data |= highByte;
                data = (data << 8) | lowByte;
                BatteryInformation batteryInfoLeftLightmodVolume;

                mutex.lock();
                batteryInfoLeftLightmodVolume = batteryInformationMap.value(itBatteryDeviceMap.key());
                mutex.unlock();
                batteryInfoLeftLightmodVolume.batteryLeftLightmodVolume = (double)data;

                mutex.lock();
                batteryInformationMap[itBatteryDeviceMap.key()] = batteryInfoLeftLightmodVolume;
                mutex.unlock();

                qDebug() << "BatteryMonitor::receiveData ==== 电池组剩余容量receiveBatteryData[4]："\
                         << dataIn.at(4);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组剩余容量receiveBatteryData[5]："\
                         << dataIn.at(5);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组剩余容量:" << data;
            }
        }
        // 电池组循环次数，数据两字节（单位：次）
        else if( dataIn.at(2) == 0x17)
        {
            if( dataIn.at(3) != 0x02)
            {
                qDebug() << "BatteryMonitor::receiveData ==== 电池组循环次数数据长度错误:" \
                         << dataIn.at(3);
                return -1;
            }
            else
            {
                // 数据低字节
                lowByte = dataIn.at(4);
                // 数据低字节
                highByte = dataIn.at(5);
                data |= highByte;
                data = (data << 8) | lowByte;
                BatteryInformation batteryInfoCycleNumber;

                mutex.lock();
                batteryInfoCycleNumber = batteryInformationMap.value(itBatteryDeviceMap.key());
                mutex.unlock();
                batteryInfoCycleNumber.batteryCycleNumber = (double)data;

                mutex.lock();
                batteryInformationMap[itBatteryDeviceMap.key()] = batteryInfoCycleNumber;
                mutex.unlock();

                qDebug() << "BatteryMonitor::receiveData ==== 电池组循环次数receiveBatteryData[4]："\
                         << dataIn.at(4);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组循环次数receiveBatteryData[5]："\
                         << dataIn.at(5);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组循环次数:" << data;
            }
        }
        // 电池组设计容量，数据两字节（单位：mAh）
        else if(dataIn.at(2) == 0x18)
        {
            if( dataIn.at(3) != 0x02)
            {
                qDebug() << "BatteryMonitor::receiveData ==== 电池组设计容量数据长度错误:" \
                         << dataIn.at(3);
                return -1;
            }
            else
            {
                // 数据低字节
                lowByte = dataIn.at(4);
                // 数据低字节
                highByte = dataIn.at(5);
                data |= highByte;
                data = (data << 8) | lowByte;
                BatteryInformation batteryInfoDesignVolume;

                mutex.lock();
                batteryInfoDesignVolume = batteryInformationMap.value(itBatteryDeviceMap.key());
                mutex.unlock();
                batteryInfoDesignVolume.batteryDesignVolume = (double)data;

                mutex.lock();
                batteryInformationMap[itBatteryDeviceMap.key()] = batteryInfoDesignVolume;
                mutex.unlock();

                qDebug() << "BatteryMonitor::receiveData ==== 电池组设计容量receiveBatteryData[4]："\
                         << dataIn.at(4);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组设计容量receiveBatteryData[5]："\
                         << dataIn.at(5);
                qDebug() << "BatteryMonitor::receiveData ==== 电池组设计容量:" << data;
            }
        }
        // 电池组7节电池各电压
        else if(dataIn.at(2) == 0x24)
        {
            if( dataIn.at(3) != 0x0e)
            {
                qDebug() << "BatteryMonitor::receiveData ==== 电池组7节电池各电压数据长度错误:"\
                         << dataIn.at(3);
                return -1;
            }
            else
            {
                std::vector <double> onePartVoltage;
                for(int i = 4; i < 4 + 0x0e; i+=2)
                {
                    lowByte = dataIn.at(i);
                    highByte = dataIn.at(i+1);
                    data |= highByte;
                    data = (data << 8) | lowByte;
                    onePartVoltage.push_back( (double)data);
                    qDebug() << "BatteryMonitor::receiveData ==== 电池组各节电池电压:" <<data;
                }

                BatteryInformation batteryInfoOnePartVoltage;

                mutex.lock();
                batteryInfoOnePartVoltage = batteryInformationMap.value(itBatteryDeviceMap.key());
                mutex.unlock();
                batteryInfoOnePartVoltage.batteryOnePartVoltage = onePartVoltage;

                mutex.lock();
                batteryInformationMap[itBatteryDeviceMap.key()] = batteryInfoOnePartVoltage;
                mutex.unlock();
            }
        }
        else
        {
            qDebug() << "BatteryMonitor::receiveData ==== 指令不属于电池组通讯指令：" << dataIn.at(2);
        }

    }
    requestCount=0;
    return 1;
}

int BatteryMonitor::shangHaiJinQu_ReceiveData(QByteArray &dataIn)
{
    if(34!=dataIn.size())
    {
        return -1;
    }

    unsigned char highByte = 0x00;
    unsigned char lowByte = 0x00;
    unsigned short data = 0;

    if((unsigned char)dataIn.at(0) != 0xdd
            || (unsigned char)dataIn.at(1) != 0xa5
            || (unsigned char)dataIn.at(2) != 0x00 )
    {
        qDebug() << "BatteryMonitor::receiveData() ==== 发送指令：dataIn.at(1)" << dataIn.at(1);
        qDebug() << "BatteryMonitor::receiveData() ==== 接收数据指令不符dataIn.at(2)："<< dataIn.at(2);
        QTime dieTime = QTime::currentTime().addMSecs(200);
        while( QTime::currentTime() < dieTime )
        {
            QCoreApplication::processEvents(QEventLoop::AllEvents, 200);
        }
        return -1;
    }

    //检查校验数据
    unsigned short tmpCheck1=0;
    unsigned short tmpCheck2=0;
//    qDebug()<<(unsigned char)dataIn.at(3+(unsigned char)dataIn.at(3)+1);
//    qDebug()<<(unsigned char)dataIn.at(3+(unsigned char)dataIn.at(3)+2);

    tmpCheck2 =dataIn.at(3+(unsigned char)dataIn.at(3)+1)<<8;
    tmpCheck2 += (unsigned char)dataIn.at(3+(unsigned char)dataIn.at(3)+2);
    for(int i=0;i<=(unsigned char)dataIn.at(3);i++)
    {
        tmpCheck1+=(unsigned char)dataIn.at(3+i);
    }
    tmpCheck1=~tmpCheck1+1;
    if(tmpCheck1!=tmpCheck2)
    {
        qDebug() << "BatteryMonitor::shangHaiJinQu_ReceiveData 检查校验数据check error ,tmpCheck1="
                 << tmpCheck1<<"tmpCheck2="<<tmpCheck2;//界面卡和这里有关联，
        return -2;
    }

    mutex.lock();
    // 电池组总电压，数据两字节
    // 数据低字节
    lowByte = dataIn.at(5);
    // 数据高字节
    highByte = dataIn.at(4);
    data = (highByte << 8) | lowByte;
    batteryInformationMap[itBatteryDeviceMap.key()].batterySumVoltage = ((double)data)/100.0;

    // 电池组电流，数据两字节
    signed short tmpData;
    // 数据低字节
    lowByte = dataIn.at(7);
    // 数据高字节
    highByte = dataIn.at(6);
    tmpData = (highByte << 8) | lowByte;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryCurrentCurrency = ((double)tmpData)/100.0;

    // 电池组剩余容量，数据两字节
    // 数据低字节
    lowByte = dataIn.at(9);
    // 数据高字节
    highByte = dataIn.at(8);
    data = (highByte << 8) | lowByte;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryLeftLightmodVolume = ((double)data)/100.0;

    // 电池组设计容量，数据两字节
    // 数据低字节
    lowByte = dataIn.at(11);
    // 数据高字节
    highByte = dataIn.at(10);
    data = (highByte << 8) | lowByte;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryDesignVolume = ((double)data)/100.0;


    // 电池组循环次数，数据两字节
    // 数据低字节
    lowByte = dataIn.at(13);
    // 数据高字节
    highByte = dataIn.at(12);
    data = (highByte << 8) | lowByte;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryCycleNumber = data;

    // 电池制造日期，数据两字节
    // 数据低字节
    lowByte = dataIn.at(15);
    // 数据高字节
    highByte = dataIn.at(14);
    data = (highByte << 8) | lowByte;
    batteryInformationMap[itBatteryDeviceMap.key()].manufatureData = data;

    // 电池均衡状态，数据4字节
     unsigned int tmpInt;
    // 数据低字节
    lowByte = dataIn.at(17);
    // 数据高字节
    highByte = dataIn.at(16);
    tmpInt = (highByte << 8) | lowByte;
    // 数据低字节
    lowByte = dataIn.at(19);
    // 数据高字节
    highByte = dataIn.at(18);
    tmpInt = (highByte << 24) | (lowByte<< 16);
    batteryInformationMap[itBatteryDeviceMap.key()].balanceStatus = tmpInt;

    // 电池安全状态，数据两字节
    // 数据低字节
    lowByte = dataIn.at(21);
    // 数据高字节
    highByte = dataIn.at(20);
    data = (highByte << 8) | lowByte;
    batteryInformationMap[itBatteryDeviceMap.key()].errorStatus = data;

    // 电池软件版本，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].softWareVersition = dataIn.at(22);

    // 电池剩余容量百分比，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].batteryLeftLightmodRatio =
            ((double)dataIn.at(23))/100.0;

    // 电池mos开关状态，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].fet = dataIn.at(24);

    // 电池串数，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].batterySerialCount = dataIn.at(25);

    // ntc温度传感器个数，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].ntcCount = dataIn.at(26);

    batteryInformationMap[itBatteryDeviceMap.key()].ntcData.clear();
    double tmpMaxTempeture=-100000;
    double tmpTempeture;
    for(int i=0;i<dataIn.at(26);i++)
    {
        // 数据低字节
        lowByte = dataIn.at(28+2*i);
        // 数据高字节
        highByte = dataIn.at(27+2*i);
        data = (highByte << 8) | lowByte;
        tmpTempeture=((double)data-2731)/10.0;
        batteryInformationMap[itBatteryDeviceMap.key()].ntcData.push_back(tmpTempeture);
        if(tmpTempeture>tmpMaxTempeture)
        {
            tmpMaxTempeture=tmpTempeture;
        }
    }
    batteryInformationMap[itBatteryDeviceMap.key()].batteryTempture=tmpMaxTempeture;
    mutex.unlock();
    requestCount=0;

    return 1;
}

int BatteryMonitor::shangHaiJinQu_bufferCheck(QByteArray &dataIn)
{
    static QByteArray receiveBatteryData;
    receiveBatteryData+=dataIn;
    if(receiveBatteryData.size()>100)
    {
        receiveBatteryData.clear();
        return -1;
    }

    for(int i=0;i<receiveBatteryData.size()-2;i++)
    {
        if((unsigned char)receiveBatteryData.at(i) == 0xdd
                && (unsigned char)receiveBatteryData.at(i+1) == 0xa5
                && (unsigned char)receiveBatteryData.at(i+2) == 0x00 )
        {
            if(receiveBatteryData.size()-i>=34)
            {
                dataIn.resize(34);
                for(int k=0;k<34;k++)
                {
                    dataIn[k]=receiveBatteryData[i+k];
                }
                return 1;
            }
            else
            {
                //个数不足
                return -2;
            }

        }
        else
        {

        }

    }

    return -3;


}

int BatteryMonitor::shenZhengAoYouNeng_ReceiveData(QByteArray &dataIn)
{
    //qDebug() << "BatteryMonitor::shenZhengAoYouNeng_ReceiveData ==== 接收数据：" << dataIn.toHex();
    if(130!=dataIn.size())
    {
        return -1;
    }

    unsigned char highByte = 0x00;
    unsigned char lowByte = 0x00;
    unsigned short data = 0;

    if((unsigned char)dataIn.at(0) != 0x7e
            || (unsigned char)dataIn.at(1) != 0x30
            || (unsigned char)dataIn.at(2) != 0x30 )
    {
        qDebug() << "BatteryMonitor::receiveData() ==== 发送指令：dataIn.at(1)" << dataIn.at(1);
        qDebug() << "BatteryMonitor::receiveData() ==== 接收数据指令不符dataIn.at(2)："<< dataIn.at(2);
        QTime dieTime = QTime::currentTime().addMSecs(200);
        while( QTime::currentTime() < dieTime )
        {
            QCoreApplication::processEvents(QEventLoop::AllEvents, 200);
        }
        return -1;
    }

    //不检查校验数据


    mutex.lock();

    // 电池组设计容量，数据两字节 oooooooooooooooo
    unsigned char byteValue4,byteValue3,byteValue2,byteValue1;
//    byteValue4 = (hexToChar(dataIn.at(123)<<4) + hexToChar(dataIn.at(124));
//    byteValue3 = (hexToChar(dataIn.at(123)<<4) + hexToChar(dataIn.at(124));
    byteValue2=(asciiToValue(dataIn.at(123))<<4) + asciiToValue(dataIn.at(124));
    byteValue1=(asciiToValue(dataIn.at(125))<<4) + asciiToValue(dataIn.at(126));
    data = (byteValue2 << 8) | byteValue1;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryDesignVolume = ((double)data)/100.0;

    // 电池剩余容量百分比，数据1字节 oooooooooooo
//    unsigned char tmpV1=hexToChar(dataIn.at(117));
//    tmpV1=tmpV1<<4;
    data = (asciiToValue(dataIn.at(117))<<4) + asciiToValue(dataIn.at(118));;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryLeftLightmodRatio =
            ((double)data)/100.0;

    // 电池组剩余容量，数据4字节
    byteValue4=(asciiToValue(dataIn.at(109))<<4) + asciiToValue(dataIn.at(110));
    byteValue3=(asciiToValue(dataIn.at(111))<<4) + asciiToValue(dataIn.at(112));
    byteValue2=(asciiToValue(dataIn.at(113))<<4) + asciiToValue(dataIn.at(114));
    byteValue1=(asciiToValue(dataIn.at(115))<<4) + asciiToValue(dataIn.at(116));
    unsigned int leftVolume = (byteValue4 << 24) | (byteValue3 << 16) | (byteValue2 << 8) | byteValue1;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryLeftLightmodVolume = ((double)leftVolume)/100.0;

    // 电池组电流，数据两字节
    byteValue2=(asciiToValue(dataIn.at(105))<<4) + asciiToValue(dataIn.at(106));
    byteValue1=(asciiToValue(dataIn.at(107))<<4) + asciiToValue(dataIn.at(108));
    short currency;
    currency = (byteValue2 << 8) | byteValue1;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryCurrentCurrency = ((double)currency)/100.0;

    // 电池组电压，数据两字节
    byteValue2=(asciiToValue(dataIn.at(101))<<4) + asciiToValue(dataIn.at(102));
    byteValue1=(asciiToValue(dataIn.at(103))<<4) + asciiToValue(dataIn.at(104));
    data = (byteValue2 << 8) | byteValue1;
    batteryInformationMap[itBatteryDeviceMap.key()].batterySumVoltage = ((double)data)/1000.0;

    // 电池组PCB温度，数据两字节
    byteValue2=(asciiToValue(dataIn.at(93))<<4) + asciiToValue(dataIn.at(94));
    byteValue1=(asciiToValue(dataIn.at(95))<<4) + asciiToValue(dataIn.at(96));
    data = (byteValue2 << 8) | byteValue1;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryTempture=data;


    // 电池组循环次数，数据两字节
    batteryInformationMap[itBatteryDeviceMap.key()].batteryCycleNumber = 0;

    // 电池制造日期，数据两字节
    batteryInformationMap[itBatteryDeviceMap.key()].manufatureData = 0;

    // 电池均衡状态，数据4字节
    batteryInformationMap[itBatteryDeviceMap.key()].balanceStatus = 0;

    // 电池安全状态，数据两字节
    batteryInformationMap[itBatteryDeviceMap.key()].errorStatus = 0;

    // 电池软件版本，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].softWareVersition = 0;

    // 电池mos开关状态，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].fet = 0;

    // 电池串数，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].batterySerialCount = 0;

    // ntc温度传感器个数，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].ntcCount = 0;

    batteryInformationMap[itBatteryDeviceMap.key()].ntcData.clear();

    mutex.unlock();
    requestCount=0;

    return 1;
}

int BatteryMonitor::shenZhengAoYouNeng_bufferCheck(QByteArray &dataIn)
{
    static QByteArray receiveBatteryData;
    receiveBatteryData+=dataIn;
    if(receiveBatteryData.size()>300)
    {
        receiveBatteryData.clear();
        return -1;
    }

    for(int i=0;i<receiveBatteryData.size()-2;i++)
    {
        if((unsigned char)receiveBatteryData.at(i) == 0x7e
                && (unsigned char)receiveBatteryData.at(i+1) == 0x30
                && (unsigned char)receiveBatteryData.at(i+2) == 0x30 )
        {
            if(receiveBatteryData.size()-i>=D_STAND_RECEIVE_LENGTH_AOYOU)
            {
                dataIn.resize(D_STAND_RECEIVE_LENGTH_AOYOU);
                for(int k=0;k<D_STAND_RECEIVE_LENGTH_AOYOU;k++)
                {
                    dataIn[k]=receiveBatteryData[i+k];
                }
                return 1;
            }
            else
            {
                //个数不足
                return -2;
            }

        }
        else
        {

        }

    }

    return -3;

}

int BatteryMonitor::bufferCheck_PeiChengChuNeng(QByteArray &dataIn)
{
    static QByteArray receiveBatteryData;
    receiveBatteryData+=dataIn;
    if(receiveBatteryData.size()>1000)
    {
        receiveBatteryData.clear();
        return -1;
    }

    for(int i=0;i<receiveBatteryData.size()-4;i++)
    {
        if((unsigned char)receiveBatteryData.at(i) == 0x7e
                && (unsigned char)receiveBatteryData.at(i+3) == 0x30
                && (unsigned char)receiveBatteryData.at(i+4) == 0x30 )
        {

            int endIndex=-1;
            for(int k=i;k<receiveBatteryData.size();k++)
            {
                if(0x0d==(unsigned char)receiveBatteryData.at(k))
                {
                    endIndex=k;
                }
            }

            if(endIndex>=0)
            {
                dataIn.resize(endIndex-i+1);
                for(int k=0;k<dataIn.size();k++)
                {
                    dataIn[k]=receiveBatteryData[i+k];
                }
                return 1;
            }
            else
            {
                //个数不足
                return -2;
            }

        }
        else
        {

        }

    }

    return -3;

}

int BatteryMonitor::bufferCheck_ShangDongJingGong(QByteArray &dataIn)
{
//    static QByteArray receiveBatteryData;
//    receiveBatteryData+=dataIn;
//    if(receiveBatteryData.size()>1000)
//    {
//        receiveBatteryData.clear();
//        return -1;
//    }

//    for(int i=0;i<receiveBatteryData.size()-4;i++)
//    {
//        if((unsigned char)receiveBatteryData.at(i) == 0x7e
//                && (unsigned char)receiveBatteryData.at(i+3) == 0x30
//                && (unsigned char)receiveBatteryData.at(i+4) == 0x30 )
//        {

//            int endIndex=-1;
//            for(int k=i;k<receiveBatteryData.size();k++)
//            {
//                if(0x0d==(unsigned char)receiveBatteryData.at(k))
//                {
//                    endIndex=k;
//                }
//            }

//            if(endIndex>=0)
//            {
//                dataIn.resize(endIndex-i+1);
//                for(int k=0;k<dataIn.size();k++)
//                {
//                    dataIn[k]=receiveBatteryData[i+k];
//                }
//                return 1;
//            }
//            else
//            {
//                //个数不足
//                return -2;
//            }

//        }
//        else
//        {

//        }

//    }

//    return -3;
}

int BatteryMonitor::receiveData_PeiChengChuNeng(QByteArray &dataIn)
{
//    qDebug() << "BatteryMonitor::receiveData_PeiChengChuNeng ==== 接收数据：" << dataIn.toHex();
    if(10>dataIn.size())
    {
        return -1;
    }

    unsigned char highByte = 0x00;
    unsigned char lowByte = 0x00;
    unsigned short data = 0;

    if((unsigned char)dataIn.at(0) != 0x7e
            || (unsigned char)dataIn.at(3) != 0x30
            || (unsigned char)dataIn.at(4) != 0x30 )
    {
        qDebug() << "BatteryMonitor::receiveData() ==== 发送指令：dataIn.at(1)" << dataIn.at(1);
        qDebug() << "BatteryMonitor::receiveData() ==== 接收数据指令不符dataIn.at(2)："<< dataIn.at(2);
        QTime dieTime = QTime::currentTime().addMSecs(200);
        while( QTime::currentTime() < dieTime )
        {
            QCoreApplication::processEvents(QEventLoop::AllEvents, 200);
        }
        return -1;
    }

    //不检查校验数据


    mutex.lock();
    //默认１个ｐａｃｋ，电池节数和温度传感器数量会变，导致数据长度也会变化。
    int batteryNum;
    batteryNum=(asciiToValue(dataIn.at(17))<<4) + asciiToValue(dataIn.at(18));
    int temperatureSensorNum;
    temperatureSensorNum=(asciiToValue(dataIn.at(19+batteryNum*4))<<4) +
                    asciiToValue(dataIn.at(20+batteryNum*4));

    unsigned char byteValue2,byteValue1;

    // 电池组PCB温度，数据两字节
    byteValue2=(asciiToValue(dataIn.at(21+batteryNum*4))<<4) + asciiToValue(dataIn.at(22+batteryNum*4));
    byteValue1=(asciiToValue(dataIn.at(23+batteryNum*4))<<4) + asciiToValue(dataIn.at(24+batteryNum*4));
    data = (byteValue2 << 8) | byteValue1;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryTempture=(data-2730)/10.0;

    int commonIndex=21+batteryNum*4+temperatureSensorNum*4;
    // 电池组电流，数据两字节
    byteValue2=(asciiToValue(dataIn.at(commonIndex))<<4) + asciiToValue(dataIn.at(commonIndex+1));
    byteValue1=(asciiToValue(dataIn.at(commonIndex+2))<<4) + asciiToValue(dataIn.at(commonIndex+3));
    short currency;
    currency = (byteValue2 << 8) | byteValue1;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryCurrentCurrency = ((double)currency)/100.0;

    // 电池组电压，数据两字节
    byteValue2=(asciiToValue(dataIn.at(commonIndex+4))<<4) + asciiToValue(dataIn.at(commonIndex+5));
    byteValue1=(asciiToValue(dataIn.at(commonIndex+6))<<4) + asciiToValue(dataIn.at(commonIndex+7));
    data = (byteValue2 << 8) | byteValue1;
    batteryInformationMap[itBatteryDeviceMap.key()].batterySumVoltage = ((double)data)/1000.0;

    // 电池组剩余容量，数据4字节
    byteValue2=(asciiToValue(dataIn.at(commonIndex+8))<<4) + asciiToValue(dataIn.at(commonIndex+9));
    byteValue1=(asciiToValue(dataIn.at(commonIndex+10))<<4) + asciiToValue(dataIn.at(commonIndex+11));
    data = (byteValue2 << 8) | byteValue1;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryLeftLightmodVolume = ((double)data)/100.0;

    // 电池组循环次数，数据两字节
    byteValue2=(asciiToValue(dataIn.at(commonIndex+18))<<4) + asciiToValue(dataIn.at(commonIndex+19));
    byteValue1=(asciiToValue(dataIn.at(commonIndex+20))<<4) + asciiToValue(dataIn.at(commonIndex+21));
    data = (byteValue2 << 8) | byteValue1;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryCycleNumber = data;



    // 电池组设计容量，数据两字节 oooooooooooooooo
    byteValue2=(asciiToValue(dataIn.at(commonIndex+22))<<4) + asciiToValue(dataIn.at(commonIndex+23));
    byteValue1=(asciiToValue(dataIn.at(commonIndex+24))<<4) + asciiToValue(dataIn.at(commonIndex+24));
    data = (byteValue2 << 8) | byteValue1;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryDesignVolume = ((double)data)/100.0;

    // 电池剩余容量百分比
    batteryInformationMap[itBatteryDeviceMap.key()].batteryLeftLightmodRatio =
            batteryInformationMap[itBatteryDeviceMap.key()].batteryLeftLightmodVolume /batteryInformationMap[itBatteryDeviceMap.key()].batteryDesignVolume;




    // 电池制造日期，数据两字节
    batteryInformationMap[itBatteryDeviceMap.key()].manufatureData = 0;

    // 电池均衡状态，数据4字节
    batteryInformationMap[itBatteryDeviceMap.key()].balanceStatus = 0;

    // 电池安全状态，数据两字节
    batteryInformationMap[itBatteryDeviceMap.key()].errorStatus = 0;

    // 电池软件版本，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].softWareVersition = 0;

    // 电池mos开关状态，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].fet = 0;

    // 电池串数，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].batterySerialCount = batteryNum;

    // ntc温度传感器个数，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].ntcCount = temperatureSensorNum;

    batteryInformationMap[itBatteryDeviceMap.key()].ntcData.clear();

    mutex.unlock();
    requestCount=0;

    return 1;
}

int BatteryMonitor::receiveData_ShangDongJingGong(QByteArray &dataIn)
{
    //提取电压段
    for(int i=0;i<dataIn.size()-10;i++)
    {
        if((unsigned char)dataIn.at(i) == 0xea
                && (unsigned char)dataIn.at(i+1) == 0xd1
                && (unsigned char)dataIn.at(i+2) == 0x01
                && (unsigned char)dataIn.at(i+5) == 0x02)
        {
            QByteArray voltageData;
            for(int k=i;k<dataIn.size();k++)
            {
                voltageData.append(dataIn[k]);
            }
            getVoltage_ShangDongJingGong(voltageData);

        }

    }

    //提取容量段
    for(int i=0;i<dataIn.size()-10;i++)
    {
        if((unsigned char)dataIn.at(i) == 0xea
                && (unsigned char)dataIn.at(i+1) == 0xd1
                && (unsigned char)dataIn.at(i+2) == 0x01
                && (unsigned char)dataIn.at(i+5) == 0x04)
        {
            QByteArray voltageData;
            for(int k=i;k<dataIn.size();k++)
            {
                voltageData.append(dataIn[k]);
            }
            getVolume_ShangDongJingGong(voltageData);

        }

    }

    //提取基本状态段
    for(int i=0;i<dataIn.size()-10;i++)
    {
        if((unsigned char)dataIn.at(i) == 0xea
                && (unsigned char)dataIn.at(i+1) == 0xd1
                && (unsigned char)dataIn.at(i+2) == 0x01
                && (unsigned char)dataIn.at(i+5) == 0x03)
        {
            QByteArray voltageData;
            for(int k=i;k<dataIn.size();k++)
            {
                voltageData.append(dataIn[k]);
            }
            getStatus_ShangDongJingGong(voltageData);

        }

    }

}

int BatteryMonitor::getVoltage_ShangDongJingGong(QByteArray &dataIn)
{

    if(dataIn.size()<11)
    {
//        qDebug()<<"222battery failed ,dataIn"<<dataIn.size();
        return -1;
    }
//    qDebug()<<"222battery sucess,dataIn"<<dataIn.size();
    unsigned char highByte = 0x00;
    unsigned char lowByte = 0x00;
    unsigned short data = 0;

    //不检查校验数据

    mutex.lock();

//    // 电池组总电压，数据两字节
//    // 数据低字节
//    lowByte = dataIn.at(10);
//    // 数据高字节
//    highByte = dataIn.at(9);
//    data = (highByte << 8) | lowByte;
//    batteryInformationMap[itBatteryDeviceMap.key()].batterySumVoltage = ((double)data)/100.0;



    // 电池串数，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].batterySerialCount = dataIn.at(8);

    // ntc温度传感器个数，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].ntcCount = dataIn.at(7);
    mutex.unlock();
    requestCount=0;
}

int BatteryMonitor::getStatus_ShangDongJingGong(QByteArray &dataIn)
{
    if(32>dataIn.size())
    {
//        qDebug()<<"333battery FAILED,dataIn"<<dataIn.size();
        return -1;
    }
//    qDebug()<<"333battery sucess,dataIn"<<dataIn.size();
    unsigned char highByte = 0x00;
    unsigned char lowByte = 0x00;
    unsigned short data = 0;


    mutex.lock();

    // 电池组电流，数据两字节
    signed short tmpData;
    // 数据低字节
    lowByte = dataIn.at(8);
    // 数据高字节
    highByte = dataIn.at(7);
    tmpData = (highByte << 8) | lowByte;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryCurrentCurrency = ((double)tmpData)/100.0;



    // 电池软件版本，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].softWareVersition = dataIn.at(25);


    batteryInformationMap[itBatteryDeviceMap.key()].ntcData.clear();

    batteryInformationMap[itBatteryDeviceMap.key()].batteryTempture=dataIn.at(14)-40;
    mutex.unlock();
    requestCount=0;

    return 1;
}

int BatteryMonitor::getVolume_ShangDongJingGong(QByteArray &dataIn)
{
    if(55>dataIn.size())
    {
//       qDebug()<<"444battery FAILED,dataIn"<<dataIn.size();
        return -1;
    }
//    qDebug()<<"444battery sucess,dataIn"<<dataIn.size();
    unsigned char highByte = 0x00;
    unsigned char lowByte = 0x00;
    unsigned short data = 0;


    mutex.lock();

    // 电池组总电压，数据两字节
    // 数据低字节
    lowByte = dataIn.at(48);
    // 数据高字节
    highByte = dataIn.at(47);
    data = (highByte << 8) | lowByte;
    batteryInformationMap[itBatteryDeviceMap.key()].batterySumVoltage = ((double)data)/100.0;


    // 电池组循环次数，数据两字节
    // 数据低字节
    lowByte = dataIn.at(10);
    // 数据高字节
    highByte = dataIn.at(9);
    data = (highByte << 8) | lowByte;
    batteryInformationMap[itBatteryDeviceMap.key()].batteryCycleNumber = data;

    // 电池剩余容量百分比，数据1字节
    batteryInformationMap[itBatteryDeviceMap.key()].batteryLeftLightmodRatio =
            ((double)dataIn.at(7))/100.0;


    // 电池组设计容量，数据4字节
     unsigned int tmpInt;
    // 数据低字节
    lowByte = dataIn.at(16);
    // 数据高字节
    highByte = dataIn.at(15);
    tmpInt = (highByte << 8) | lowByte;
    // 数据低字节
    lowByte = dataIn.at(13);
    // 数据高字节
    highByte = dataIn.at(12);
    tmpInt = (highByte << 24) | (lowByte<< 16);
    batteryInformationMap[itBatteryDeviceMap.key()].batteryDesignVolume = ((double)data)/100.0;

    // 电池组剩余容量，数据4字节
    // 数据低字节
    lowByte = dataIn.at(28);
    // 数据高字节
    highByte = dataIn.at(27);
    tmpInt = (highByte << 8) | lowByte;
    // 数据低字节
    lowByte = dataIn.at(25);
    // 数据高字节
    highByte = dataIn.at(24);
    tmpInt = (highByte << 24) | (lowByte<< 16);
    batteryInformationMap[itBatteryDeviceMap.key()].batteryLeftLightmodVolume = ((double)data)/100.0;


    mutex.unlock();
    requestCount=0;

    return 1;
}

int BatteryMonitor::updateBatteryStatus(double lightModeRatioIn,E_BATTERY_STATUS &statusOut)
{

    lightModeRatioIn=lightModeRatioIn*100.0;
    if(lightModeRatioIn > batteryPeak->warnningBatteryLeftLightmode)
    {
        batteryStatus = E_BATTERY_STATUS_NORMAL;
    }
    else if( (lightModeRatioIn <= batteryPeak->warnningBatteryLeftLightmode) && \
             (lightModeRatioIn > batteryPeak->goChargeBatteryLeftLightmode))
    {
        batteryStatus = E_BATTERY_STATUS_WARNNING;
    }
    else if((lightModeRatioIn <= batteryPeak->goChargeBatteryLeftLightmode) && \
            (lightModeRatioIn > batteryPeak->shutDownBatteryLeftLightmode))
    {
        batteryStatus = E_BATTERY_STATUS_NEED_GO_CHARGE;
    }
    else if( lightModeRatioIn <= batteryPeak->shutDownBatteryLeftLightmode)
    {
        batteryStatus = E_BATTERY_STATUS_SHUTDOWN;
    }

    return 1;
}


int BatteryMonitor::initialModbusMaster(SerialPortConfig *serialPortConfigIn,int aliasIdIn)
{

    if(NULL==modbusMaster)
    {
        QString tmpName="/dev/";
        serialPortConfigIn->serialDeviceName=tmpName+serialPortConfigIn->serialDeviceName;

        QString tmpParity;
        if(QSerialPort::NoParity==serialPortConfigIn->parity)
        {
            tmpParity='N';
        }
        else if(QSerialPort::EvenParity==serialPortConfigIn->parity)
        {
            tmpParity='E';
        }
        else if(QSerialPort::OddParity==serialPortConfigIn->parity)
        {
            tmpParity='O';
        }

        modbusMaster =modbus_new_rtu(serialPortConfigIn->serialDeviceName.toStdString().data(),
                                     serialPortConfigIn->baudRate, (*tmpParity.toStdString().data()),
                                            serialPortConfigIn->dataBits, serialPortConfigIn->stopBits);
        modbus_set_slave(modbusMaster,aliasIdIn);
        if (modbus_connect(modbusMaster) == -1)
        {
            qDebug()<<"BatteryMonitor::connectPort connect failed";
            modbus_free(modbusMaster);
            modbusMaster=NULL;
            return -2;
        }
        else
        {
            modbus_set_response_timeout(modbusMaster, 0, 500000);
//            qDebug()<<"connect success";
            return 1;
        }
    }


    return 0;
}


/**
 * @brief BatteryMonitor::initalSerialPort  串口配置初始化
 * @return      success:1  failed:-1
 */
int BatteryMonitor::initalSerialPort()
{
    bool isFinded=false;
    /**
     * @brief serialPortInfo            串口信息对象
     */
    QSerialPortInfo serialPortInfo;
    foreach ( serialPortInfo, serialPortInfo.availablePorts() )
    {
        qDebug() << "Name : " << serialPortInfo.portName();
        qDebug() << "Description : " << serialPortInfo.description();
        qDebug() << "serialNumber: " << serialPortInfo.serialNumber();
        qDebug() << "productIdentifier:" << serialPortInfo.productIdentifier();
        if(1==serialPortConfig->isUseUsbDeviceSerialCode)
        {
            if( serialPortInfo.serialNumber() == serialPortConfig->serialDeviceName)
            {
                // 设置端口号
                serialPort_device->setPortName(serialPortInfo.portName());
                qDebug()<<"find COM"<<serialPortInfo.portName();
                isFinded=true;
                break;
            }
        }
        else
        {
            if( serialPortInfo.portName() == serialPortConfig->serialDeviceName )
            {
                // 设置端口号
                serialPort_device->setPortName(serialPortConfig->serialDeviceName);
                qDebug()<<"find COM"<<serialPortInfo.portName();
                isFinded=true;
                break;
            }
        }

    }
    if(false==isFinded)
    {
        return -1;
    }
    qDebug() << "serilaPortName:" << serialPortInfo.portName();

    if( serialPort_device->open(QIODevice::ReadWrite) )
    {
       // 配置串口参数
       serialPort_device->setBaudRate(serialPortConfig->baudRate);         //　波特率
       serialPort_device->setDataBits(serialPortConfig->dataBits);         //  数据位
       serialPort_device->setParity(serialPortConfig->parity);             //  校验位
       serialPort_device->setStopBits(serialPortConfig->stopBits);         //  停止位
       serialPort_device->setFlowControl(QSerialPort::NoFlowControl);      //  流控制
       qDebug() << "串口打开成功:" << serialPort_device->portName();
   }
   else
    {
       qDebug() << "串口打开失败:" << serialPort_device->portName();
        return -1;
    }

    return 1;
}

/**
 * @brief BatteryMonitor::getBatterySumVoltage  获取电源总电压(串联，电压相加)
 * @param voltageOut
 * @return
 */
int BatteryMonitor::getBatterySumVoltage(double &voltageOut)    //mV
{
    if( batteryInformationMap.isEmpty() )
    {
        qDebug() << "getBatterySumVoltage() ==== 容器batteryInformationMap为空！";
        return -1;
    }
    QMap <int, BatteryInformation>::iterator it;

    QList <double> batterySumVoltageList;
    mutex.lock();
    for(it = batteryInformationMap.begin(); it != batteryInformationMap.end(); it++)
    {
        batterySumVoltageList << batteryInformationMap.value( it.key() ).batterySumVoltage;
    }
    mutex.unlock();
    for(int i = 0; i < batterySumVoltageList.size(); i++)
    {
        voltageOut += batterySumVoltageList.at(i);
    }

    return 1;
}

/**
 * @brief BatteryMonitor::getBatterySumVoltage
 * @param batteryModuleId
 * @param voltageOut
 * @return
 */
int BatteryMonitor::getBatterySumVoltage(int batteryModuleId,double &voltageOut)    //mV
{
    mutex.lock();
    if( false == batteryInformationMap.contains(batteryModuleId) )
    {
        qDebug() << "BatteryMonitor::getBatterySumVoltage ==== 无数据设备id:" << batteryModuleId;
        mutex.unlock();
        return -1;
    }

    voltageOut = batteryInformationMap.value(batteryModuleId).batterySumVoltage;
    mutex.unlock();
    qDebug() << "BatteryMonitor::getBatterySumVoltage ==== 设备id:" << batteryModuleId\
             << "总电压：" << voltageOut ;

    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryCurrentCurrency     获取电流（串联，电流相加）
 * @param currencyOut           电流
 * @return
 */
int BatteryMonitor::getBatteryCurrentCurrency(double &currencyOut)  //mA
{

    if( batteryInformationMap.isEmpty() )
    {
        qDebug() << "getBatteryCurrentCurrency() ==== 容器batteryInformationMap为空！";
        return -1;
    }
    QMap <int, BatteryInformation>::iterator it;

    QList <double> batteryCurrentCurrencyList;
    mutex.lock();
    for(it = batteryInformationMap.begin(); it != batteryInformationMap.end(); it++)
    {
        batteryCurrentCurrencyList << batteryInformationMap.value( it.key() ).batteryCurrentCurrency;
    }
    mutex.unlock();
    for(int i = 0; i < batteryCurrentCurrencyList.size(); i++)
    {
        currencyOut += batteryCurrentCurrencyList.at(i);
    }

    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryCurrentCurrency
 * @param batteryModuleId
 * @param currencyOut
 * @return
 */
int BatteryMonitor::getBatteryCurrentCurrency(int batteryModuleId,double &currencyOut)//mA
{
    mutex.lock();
    if( false == batteryInformationMap.contains(batteryModuleId) )
    {
        qDebug() << "BatteryMonitor::getBatteryCurrentCurrency ==== 无设备id:" << batteryModuleId;
        mutex.unlock();
        return -1;
    }
    currencyOut = batteryInformationMap.value(batteryModuleId).batteryCurrentCurrency;
    mutex.unlock();

    qDebug() << "BatteryMonitor::getBatteryCurrentCurrency ==== 设备id:" << batteryModuleId\
             << "实时电流：" << currencyOut ;

    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryLeftLightmodRatio
 * @param ratioOut
 * @return
 */
int BatteryMonitor::getBatteryLeftLightmodRatio(double &ratioOut)       //%
{
    if( batteryInformationMap.isEmpty() )
    {
        qDebug() << "BatteryMonitor::getBatteryLeftLightmodRatio ==== 容器batteryInformationMap为空！";
        return -1;
    }
    QMap <int, BatteryInformation>::iterator it;
    QList <double> batteryLeftLightmodRatioList;
    mutex.lock();
    for(it = batteryInformationMap.begin(); it != batteryInformationMap.end(); it++)
    {
        batteryLeftLightmodRatioList << batteryInformationMap.value( it.key() ).batteryLeftLightmodRatio;
    }
    mutex.unlock();
    // 对ＱList 进行从小到大排序
    qSort(batteryLeftLightmodRatioList.begin(), batteryLeftLightmodRatioList.end());

    ratioOut = batteryLeftLightmodRatioList.first();

    qDebug() << "getBatteryLeftLightmodRatio ==== the Min batteryLeftLightmodRatio:" << ratioOut;

    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryLeftLightmodRatio
 * @param batteryModuleId
 * @param ratioOut
 * @return
 */
int BatteryMonitor::getBatteryLeftLightmodRatio(int batteryModuleId,double &ratioOut)   //%
{
    mutex.lock();
    if( false == batteryInformationMap.contains(batteryModuleId) )
    {
        qDebug() << "BatteryMonitor::getBatteryLeftLightmodRatio ==== 无设备id:" << batteryModuleId;
        mutex.unlock();
        return -1;
    }
    ratioOut = batteryInformationMap.value(batteryModuleId).batteryLeftLightmodRatio;
    mutex.unlock();
    qDebug() << "BatteryMonitor::getBatteryLeftLightmodRatio ==== 设备id:" << batteryModuleId\
             << "电池组相对容量百分比：" << ratioOut ;

    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryLeftLightmodVolume  电池组剩余容量(取最小值)
 * @param volumeOut
 * @return
 */
int BatteryMonitor::getBatteryLeftLightmodVolume(double &volumeOut)     //unit is mAh
{

    if( batteryInformationMap.isEmpty() )
    {
        qDebug() << "getBatteryLeftLightmodVolume() ==== 容器batteryInformationMap为空！";
        return -1;
    }
    QMap <int, BatteryInformation>::iterator it;
    QList <double> batteryLeftLightmodVolumeList;
    mutex.lock();
    for(it = batteryInformationMap.begin(); it != batteryInformationMap.end(); it++)
    {
        batteryLeftLightmodVolumeList << batteryInformationMap.value( it.key() ).batteryLeftLightmodVolume;
    }
    mutex.unlock();
    // 对ＱList 进行从小到大排序
    qSort(batteryLeftLightmodVolumeList.begin(), batteryLeftLightmodVolumeList.end());

    volumeOut = batteryLeftLightmodVolumeList.first();
    qDebug() << "getBatteryLeftLightmodVolume ==== the Min batteryLeftLightmodVolume:" << volumeOut;


    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryLeftLightmodVolume
 * @param batteryModuleId
 * @param volumeOut
 * @return
 */
int BatteryMonitor::getBatteryLeftLightmodVolume(int batteryModuleId,double &volumeOut) //unit is mAh
{
    mutex.lock();
    if( false == batteryInformationMap.contains(batteryModuleId) )
    {
        qDebug() << "BatteryMonitor::getBatteryLeftLightmodVolume ==== 无设备id:" << batteryModuleId;
        mutex.unlock();
        return -1;
    }
    volumeOut = batteryInformationMap.value(batteryModuleId).batteryLeftLightmodVolume;
    mutex.unlock();
    qDebug() << "BatteryMonitor::getBatteryLeftLightmodVolume ==== 设备id:" << batteryModuleId\
             << "电池组剩余容量：" << volumeOut ;

    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryCycleNumber     电池组循环次数(取最大值)
 * @param cycleNumberOut
 * @return
 */
int BatteryMonitor::getBatteryCycleNumber(double &cycleNumberOut)
{
    if( batteryInformationMap.isEmpty() )
    {
        qDebug() << "getBatteryCycleNumber() ==== 容器batteryInformationMap为空！";
        return -1;
    }
    QMap <int, BatteryInformation>::iterator it;
    QList <double> batteryCycleNumberList;
    mutex.lock();
    for(it = batteryInformationMap.begin(); it != batteryInformationMap.end(); it++)
    {
        batteryCycleNumberList << batteryInformationMap.value( it.key() ).batteryCycleNumber;
    }
    mutex.unlock();
    // 对ＱList 进行从小到大排序
    qSort(batteryCycleNumberList.begin(), batteryCycleNumberList.end());

    cycleNumberOut = batteryCycleNumberList.last();
    qDebug() << "getBatteryCycleNumber ==== the Max batteryCycleNumber:" << cycleNumberOut;

    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryCycleNumber
 * @param batteryModuleId
 * @param cycleNumberOut
 * @return
 */
int BatteryMonitor::getBatteryCycleNumber(int batteryModuleId,double &cycleNumberOut)
{
    mutex.lock();
    if( false == batteryInformationMap.contains(batteryModuleId) )
    {
        qDebug() << "BatteryMonitor::getBatteryCycleNumber ==== 无设备id:" << batteryModuleId;
        mutex.unlock();
        return -1;
    }
    cycleNumberOut = batteryInformationMap.value(batteryModuleId).batteryCycleNumber;
    mutex.unlock();
    qDebug() << "BatteryMonitor::getBatteryCycleNumber ==== 设备id:" << batteryModuleId\
             << "电池组循环次数：" << cycleNumberOut ;

    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryDesignVolume
 * @param designVolumeOut
 * @return
 */
int BatteryMonitor::getBatteryDesignVolume(double &designVolumeOut)     //unit is mAh
{
    if( batteryInformationMap.isEmpty() )
    {
        qDebug() << "getBatteryDesignVolume() ==== 容器batteryInformationMap为空！";
        return -1;
    }
    QMap <int, BatteryInformation>::iterator it;
    QList <double> batteryDesignVolumeList;
    mutex.lock();
    for(it = batteryInformationMap.begin(); it != batteryInformationMap.end(); it++)
    {
        batteryDesignVolumeList << batteryInformationMap.value( it.key() ).batteryDesignVolume;
    }
    mutex.unlock();
    // 对ＱList 进行从小到大排序
    qSort(batteryDesignVolumeList.begin(), batteryDesignVolumeList.end());

    designVolumeOut = batteryDesignVolumeList.first();
    qDebug() << "getBatteryDesignVolume ==== the Min batteryDesignVolume:" << designVolumeOut;

    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryDesignVolume
 * @param batteryModuleId
 * @param designVolumeOut
 * @return
 */
int BatteryMonitor::getBatteryDesignVolume(int batteryModuleId,double &designVolumeOut)  //unit is mAh
{
    mutex.lock();
    if( false == batteryInformationMap.contains(batteryModuleId) )
    {
        qDebug() << "BatteryMonitor::getBatteryDesignVolume ==== 无设备id:" << batteryModuleId;
        mutex.unlock();
        return -1;
    }
    designVolumeOut = batteryInformationMap.value(batteryModuleId).batteryDesignVolume;
    mutex.unlock();
    qDebug() << "BatteryMonitor::getBatteryDesignVolume ==== 设备id:" << batteryModuleId\
             << "电池组设计容量：" << designVolumeOut ;

    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryOnePartVoltage
 * @param batteryModuleId
 * @param voltageListOut
 * @return
 */
int BatteryMonitor::getBatteryOnePartVoltage(int batteryModuleId,std::vector<double> &voltageListOut)  //unit is mAh
{
    mutex.lock();
    if( false == batteryInformationMap.contains(batteryModuleId) )
    {
        qDebug() << "BatteryMonitor::getBatteryOnePartVoltage ==== 无设备id:" << batteryModuleId;
        mutex.unlock();
        return -1;
    }
    voltageListOut = batteryInformationMap.value(batteryModuleId).batteryOnePartVoltage;
    mutex.unlock();
    qDebug() << "BatteryMonitor::getBatteryOnePartVoltage ==== 设备id:" << batteryModuleId;
    for(int i = 0; i < voltageListOut.size(); i++)
    {
        qDebug() << "电池组7节电池各电压 ==== "  << i << "=====电压:"<< voltageListOut.at(i) ;
    }

    return 1;
}

/**
 * @brief setChargeTime
 * @param chargeTimeIn
 * @return
 */
int BatteryMonitor::setChargeTime(double chargeTimeIn)
{
    DomParser domparser;
    bool ok = domparser.openXml(batteryConfigFilePath, QIODevice::ReadWrite);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setChargeTime ==== 配置文件打开失败：" << batteryConfigFilePath;
        }
        return -1;
    }

    QDomNode domNode = domparser.findSubNode(domparser.getRootDomElement(), "BatteryPeak", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setChargeTime ==== 查找BatteryPeak结点失败：" \
                     << batteryConfigFilePath;
        }
            return -1;
    }

    QDomNode chargeTimeNode = domparser.findSubNode(domNode, "chargeTime", ok);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setChargeTime ==== 查找chargeTime结点失败：" \
                     << batteryConfigFilePath;
        }
            return -1;
    }
    domparser.writeXml(chargeTimeNode,chargeTimeIn);

    domparser.closeXml(true);
    return 1;
}

/**
 * @brief setChargeVolume
 * @param chargeVolumeIn
 * @return
 */
int BatteryMonitor::setChargeVolume(double chargeVolumeIn)
{
    DomParser domparser;
    bool ok = domparser.openXml(batteryConfigFilePath, QIODevice::ReadWrite);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setChargeVolume ==== 配置文件打开失败：" << batteryConfigFilePath;
        }
        return -1;
    }

    QDomNode domNode = domparser.findSubNode(domparser.getRootDomElement(), "BatteryPeak", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setChargeVolume ==== 查找BatteryPeak结点失败：" \
                     << batteryConfigFilePath;
        }
            return -1;
    }

    QDomNode chargeTimeNode = domparser.findSubNode(domNode, "chargeVolume", ok);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setChargeVolume ==== 查找chargeVolume结点失败：" \
                     << batteryConfigFilePath;
        }
            return -1;
    }
    domparser.writeXml(chargeTimeNode,chargeVolumeIn);

    domparser.closeXml(true);
    return 1;
}

/**
 * @brief setWarnningBatteryLeftLightmode
 * @param dateIn
 * @return
 */
int BatteryMonitor::setWarnningBatteryLeftLightmode(double dateIn)
{
    DomParser domparser;
    bool ok = domparser.openXml(batteryConfigFilePath, QIODevice::ReadWrite);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setWarnningBatteryLeftLightmode ==== 配置文件打开失败："\
                     << batteryConfigFilePath;
        }
        return -1;
    }

    QDomNode domNode = domparser.findSubNode(domparser.getRootDomElement(), "BatteryPeak", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setWarnningBatteryLeftLightmode ==== 查找BatteryPeak结点失败：" \
                     << batteryConfigFilePath;
        }
            return -1;
    }

    QDomNode chargeTimeNode = domparser.findSubNode(domNode, "warnningBatteryLeftLightmode", ok);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setWarnningBatteryLeftLightmode ==== 查找warnningBatteryLeftLightmode结点失败：" \
                     << batteryConfigFilePath;
        }
            return -1;
    }
    domparser.writeXml(chargeTimeNode,dateIn);

    domparser.closeXml(true);
    return 1;
}

/**
 * @brief setGoChargeBatteryLeftLightmode
 * @param dateIn
 * @return
 */
int BatteryMonitor::setGoChargeBatteryLeftLightmode(double dateIn)
{
    DomParser domparser;
    bool ok = domparser.openXml(batteryConfigFilePath, QIODevice::ReadWrite);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setGoChargeBatteryLeftLightmode ==== 配置文件打开失败："\
                     << batteryConfigFilePath;
        }
        return -1;
    }

    QDomNode domNode = domparser.findSubNode(domparser.getRootDomElement(), "BatteryPeak", ok );
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setGoChargeBatteryLeftLightmode ==== 查找BatteryPeak结点失败：" \
                     << batteryConfigFilePath;
        }
            return -1;
    }

    QDomNode chargeTimeNode = domparser.findSubNode(domNode, "goChargeBatteryLeftLightmode", ok);
    if( !ok )
    {
        if(IF_DEBUG)
        {
            qDebug() << "BatteryMonitor::setGoChargeBatteryLeftLightmode ==== 查找goChargeBatteryLeftLightmode结点失败：" \
                     << batteryConfigFilePath;
        }
            return -1;
    }
    domparser.writeXml(chargeTimeNode,dateIn);

    domparser.closeXml(true);
    return 1;
}

/**
 * @brief getBatteryStatus  获取电池组状态
 * @param statusOut
 * @return
 */
int BatteryMonitor::getBatteryStatus(E_BATTERY_STATUS &statusOut)
{
    if(0==isUseBattery)
    {
        statusOut=E_BATTERY_STATUS_NORMAL;
        return 0;
    }
    statusOut = (E_BATTERY_STATUS)batteryStatus;
    return 1;
}

int BatteryMonitor::getShutdownDelaySeconds()
{
    return shutdownDelaySeconds;
}

int BatteryMonitor::writeConfigFile(QString configFilePath,BatteryPeakConfig configIn)
{
    qDebug() << "BatteryMonitor::writeConfigFile ==== " << configFilePath;

    DomParser domparser;
    bool ok = domparser.openXml(configFilePath, QIODevice::ReadWrite);
    if( !ok )
    {

        return -1;
    }



    QDomNode domNode = domparser.findSubNode(domparser.getRootDomElement(), "BatteryPeak", ok );
    if( !ok )
    {

            return -2;
    }

    // 读取充电时间（单位：min）
    domparser.writeXml(domparser.findSubNode(domNode, "chargeTime", ok ), configIn.chargeTime);
    if( !ok )
    {

            return -3;
    }


    // 读取充电标准的电池组相对容量比(单位：％)
    domparser.writeXml(domparser.findSubNode(domNode, "chargeVolume", ok ), configIn.chargeVolume);
    if( !ok )
    {

            return -4;
    }


    // 读取警报时的电池组相对容量比（单位：％）
    domparser.writeXml(domparser.findSubNode(domNode, "warnningBatteryLeftLightmode", ok ), \
                      configIn.warnningBatteryLeftLightmode);
    if( !ok )
    {

            return -5;
    }


    // 读取需要充电的电池组相对容量比(单位：％)
    domparser.writeXml(domparser.findSubNode(domNode, "goChargeBatteryLeftLightmode", ok ), \
                      configIn.goChargeBatteryLeftLightmode);
    if( !ok )
    {

            return -6;
    }


    // 读取需要关机时的电池组相对容量比(单位：％)
    domparser.writeXml(domparser.findSubNode(domNode, "shutDownBatteryLeftLightmode", ok ), \
                      configIn.shutDownBatteryLeftLightmode);
    if( !ok )
    {

            return -7;
    }



    domparser.closeXml(true);
    return 1;
}

/**
 * @brief getBatteryTempture  获取电池组内部温度
 * @param temptureOut
 * @return
 */
int BatteryMonitor::getBatteryTempture(double &temptureOut)//摄氏度
{
    if( batteryInformationMap.isEmpty() )
    {
        qDebug() << "getBatteryTempture() ==== 容器batteryInformationMap为空！";
        return -1;
    }
    QMap <int, BatteryInformation>::iterator it;
    QList <double> batterTemptureList;

    mutex.lock();
    for(it = batteryInformationMap.begin(); it != batteryInformationMap.end(); it++)
    {
        qDebug() << "batterTemptureList ===== 电池内部温度:" \
                 << batteryInformationMap.value( it.key() ).batteryTempture;
        batterTemptureList << batteryInformationMap.value( it.key() ).batteryTempture;
    }
    mutex.unlock();
    // 对ＱList 进行从小到大排序
    qSort(batterTemptureList.begin(), batterTemptureList.end());

    temptureOut = batterTemptureList.last();
    qDebug() << "getBatteryTempture ==== the Max batteryTempture:" << temptureOut;

    return 1;
}

/**
 * @brief BatteryMonitor::getBatteryTempture 获取电池组内部温度
 * @param batteryModuleId
 * @param temptureOut
 * @return
 */
int BatteryMonitor::getBatteryTempture(int batteryModuleId,double &temptureOut)
{
    mutex.lock();
    if( false == batteryInformationMap.contains(batteryModuleId) )
    {
        qDebug() << "BatteryMonitor::getBatteryTempture ==== 无设备id:" << batteryModuleId;
        mutex.unlock();
        return -1;
    }
    temptureOut = batteryInformationMap.value(batteryModuleId).batteryTempture;
    mutex.unlock();
    qDebug() << "BatteryMonitor::getBatteryTempture ==== 设备id:" << batteryModuleId\
             << "电池组内部温度：" << temptureOut ;

    return 1;
}
